package com.example.idachuappone.index.item;

import java.util.List;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.action.IndexTwoAction;
import com.example.idachuappone.index.adapter.AdapterFamilyFeast;
import com.example.idachuappone.index.dto.FamilyFeastDto;
import com.example.idachuappone.index.entity.FamilyFeast;
import com.example.idachuappone.index.ui.MyListView;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.CheckUtil;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class FamilyFeastActivity extends BaseActivity implements OnClickListener, OnItemClickListener {
	private AdapterFamilyFeast adapterFamilyFeast;
	private Button btn_back;
	private Button btn_info;
	private BitmapUtils bu;
	private Dialog dialogLoading;
	private ImageView img_end;
	private List<FamilyFeast> lists;
	private LinearLayout ll_no_network;
	private MyListView mlv_main;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_family_feast);
		this.bu = BitmapUtilHelper.createBitmapUtil(this);
		this.dialogLoading = ComUtil.createCusProgressSmallBlackBgDialog(this);
		initView();
		checkNet();
	}

	private void checkNet() {
		if (CheckUtil.checkNet(this)) {
			initData();
		} else {
			this.ll_no_network.setVisibility(0);
		}
	}

	private void initData() {
		this.dialogLoading.show();
		NetUtil.get(this, Constant.FEATURE, 100, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				FamilyFeastActivity.this.dialogLoading.dismiss();
				FamilyFeastActivity.this.ll_no_network
						.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				String json = NetUtil.unescapeUnicode(responseInfo.result);
				try {
					FamilyFeastActivity.this.lists = new FamilyFeastDto().getFamilyFeastList(FamilyFeastActivity.this,
							json);
					FamilyFeastActivity.this.initAdapter();
				} catch (Exception e) {
					MainToast.show(FamilyFeastActivity.this, e.getMessage(), 0);
				}
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				FamilyFeastActivity.this.dialogLoading.dismiss();
				FamilyFeastActivity.this.ll_no_network.setVisibility(0);
			}
		});
	}

	private void initAdapter() {
		this.adapterFamilyFeast.setList(this.lists);
		this.adapterFamilyFeast.notifyDataSetChanged();
		this.img_end.setVisibility(0);
	}

	private void initView() {
		this.img_end = (ImageView) findViewById(R.id.img_end);
		this.ll_no_network = (LinearLayout) findViewById(R.id.ll_no_network);
		this.ll_no_network.setOnClickListener(this);
		this.btn_info = (Button) findViewById(R.id.btn_info);
		this.btn_info.setOnClickListener(this);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.mlv_main = (MyListView) findViewById(R.id.mlv_main);
		this.adapterFamilyFeast = new AdapterFamilyFeast(this, this.bu);
		this.mlv_main.setAdapter(this.adapterFamilyFeast);
		this.mlv_main.setOnItemClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.ll_no_network /* 2131165232 */:
			checkNet();
		case R.id.btn_info /* 2131165293 */:
			startActivity(new Intent(this, FamilyFeastHelpActivity.class));
		default:
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
		FamilyFeast familyFeast = this.lists.get(position);
		new IndexTwoAction().activityGoUriorUrl(this, familyFeast.getUri(), familyFeast.getUrl(),
				familyFeast.getName(), familyFeast.isUrl());
	}
}
